package au.com.handsunited.web.taglib.relationship;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.relationship.Relationship;

/**
 * @author arunda
 *
 */
public class RelationshipRoleTag extends BodyTagSupport {

	private static final String ROLE_ADMIN = "admin";
	private static final String ROLE_NOTADMIN = "notadmin";
	private static final String ROLE_OWNER = "owner";
	private static final String ROLE_NOTOWNER = "notowner";


	private static Logger logger = LoggerFactory.getLogger(RelationshipRoleTag.class);
	private Relationship relationship;
	private BaseEntity member;
	private BaseEntity entity;
	private String role;


	/**
	 * @throws JspException
	 *
	 */
	public RelationshipRoleTag() throws JspException {
	}


	/**
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {

		RelationshipStatusTag statusTagParent = (RelationshipStatusTag) findAncestorWithClass(this, RelationshipStatusTag.class);

		if (statusTagParent != null) {
			relationship = statusTagParent.getRelationship();
			entity = statusTagParent.getEntity();
			member = statusTagParent.getMember();
		} else {
			RelationshipTag relationshipTagParent = (RelationshipTag) findAncestorWithClass(this, RelationshipTag.class);
			if (relationshipTagParent != null) {
				entity = relationshipTagParent.getEntity();
				member = relationshipTagParent.getMember();
				relationship = relationshipTagParent.getRelationship();
			} else {
				throw new JspException("role tag is not inside a relationship or a status tag");
			}
		}

		if (ROLE_ADMIN.equalsIgnoreCase(role) && (entity.isAdministratorOf(member))) {
			return EVAL_BODY_INCLUDE;
		} else if (ROLE_NOTADMIN.equalsIgnoreCase(role) && (!entity.isAdministratorOf(member))) {
			return EVAL_BODY_INCLUDE;
		} else {
			return SKIP_BODY;
		}

	}


	public Relationship getRelationship() throws JspException {

		if (relationship == null) {
			RelationshipStatusTag statusTagParent = (RelationshipStatusTag) findAncestorWithClass(this, RelationshipStatusTag.class);

			if (statusTagParent != null) {
				relationship = statusTagParent.getRelationship();
			} else {
				RelationshipTag relationshipTagParent = (RelationshipTag) findAncestorWithClass(this, RelationshipTag.class);
				if (relationshipTagParent != null) {
					relationship = relationshipTagParent.getRelationship();
				} else {
					throw new JspException("role tag is not inside a relationship or a status tag");
				}
			}
		}
		return relationship;
	}


	public void setRelationship(Relationship relationship) {
		this.relationship = relationship;
	}


	public BaseEntity getMember() throws JspException {

		if (member == null) {
			RelationshipStatusTag statusTagParent = (RelationshipStatusTag) findAncestorWithClass(this, RelationshipStatusTag.class);

			if (statusTagParent != null) {
				member = statusTagParent.getMember();
			} else {
				RelationshipTag relationshipTagParent = (RelationshipTag) findAncestorWithClass(this, RelationshipTag.class);
				if (relationshipTagParent != null) {
					member = relationshipTagParent.getMember();
				} else {
					throw new JspException("role tag is not inside a relationship or a status tag");
				}
			}
		}

		return member;
	}


	public void setMember(BaseEntity member) {
		this.member = member;
	}


	public BaseEntity getEntity() throws JspException {

		if (entity == null) {
			RelationshipStatusTag statusTagParent = (RelationshipStatusTag) findAncestorWithClass(this, RelationshipStatusTag.class);

			if (statusTagParent != null) {
				entity = statusTagParent.getEntity();
			} else {
				RelationshipTag relationshipTagParent = (RelationshipTag) findAncestorWithClass(this, RelationshipTag.class);
				if (relationshipTagParent != null) {
					entity = relationshipTagParent.getEntity();
				} else {
					throw new JspException("role tag is not inside a relationship or a status tag");
				}
			}

		}

		return entity;
	}


	public void setEntity(BaseEntity entity) {
		this.entity = entity;
	}


	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
}
